package com.patelski.magic.controller;

import java.math.BigDecimal;

import javax.ejb.Stateful;
import javax.persistence.Query;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.security.Restrict;
import org.jboss.seam.log.Log;

import com.patelski.common.controller.AbstractEntityHomeImpl;
import com.patelski.common.controller.StatefulAbstractEntityHomeImpl;
import com.patelski.magic.Authenticator;
import com.patelski.magic.model.Card;
import com.patelski.magic.model.Inventory;

@Stateful
@Name("inventoryHome")
public class InventoryHomeImpl extends
		StatefulAbstractEntityHomeImpl<Inventory> implements InventoryHome {

	@Logger
	Log log;

	@In(create = true)
	Authenticator authenticator;

	private Card card = null;
	private Integer addCardId = null;
	private int id;
	private int add;
	private int keep;
	private BigDecimal sellAmount = new BigDecimal(0.00);
	private BigDecimal spent = new BigDecimal(0.00);

	@Override
	protected Inventory createInstance() {
		Inventory inventory = new Inventory();
		return inventory;
	}

	public void addCard(Card card) {
		addCard(card, 1);
	}

	public void addCard(Card card, Integer addCopies) {
		// TEST instance()
		if (authenticator.isLoggedIn()) {
			Query q = em.createNamedQuery("cardInInventory");
			q.setParameter("card", card);
			q.setParameter("user", authenticator.getLoggedInUser());
			List<Inventory> results = q.getResultList();
			if (1 == results.size()) {
				instance = (Inventory) q.getSingleResult();
				instance.setHave(instance.getHave() + addCopies);
				this.addFacesMessage(
						"Raised the number of copies of card #0 to #1", card
								.getCardName(), instance.getHave());
			} else {
				instance = new Inventory(authenticator.getLoggedInUser(), card,
						addCopies, 0, new BigDecimal(0), new BigDecimal(0),
						new BigDecimal(0));
				this.addFacesMessage(
						"Added card #0 to inventory with #1 copies. ", card
								.getCardName(), addCopies);
			}
			em.persist(instance);
		} else {
			this
					.addFacesMessage("You must login before you can add cards to the inventory");
		}
	}

	public void addCard() {
		if (authenticator.isLoggedIn()) {
			Query q = em.createNamedQuery("cardInInventory");
			q.setParameter("card", card);
			q.setParameter("user", authenticator.getLoggedInUser());
			Inventory theInventory = new Inventory();
			List<Inventory> results = q.getResultList();
			if (1 == results.size()) {
				theInventory = (Inventory) q.getSingleResult();
				theInventory.setHave(theInventory.getHave()
						+ instance.getHave());
				theInventory.setNote(theInventory.getNote() + " - "
						+ instance.getNote());
				theInventory.setTotalAmount(theInventory.getTotalAmount().add(
						instance.getTotalAmount()));
				em.persist(theInventory);
				this.addFacesMessage(
						"Raised the number of copies of card #0 to #1", card
								.getCardName(), theInventory.getHave());
			} else {
				instance.setUser(authenticator.getLoggedInUser());
				instance.setCard(this.card);
				em.persist(instance);
				this.addFacesMessage(
						"Added card #0 to inventory with #1 copies. ", card
								.getCardName(), instance.getHave());
			}
		} else {
			this
					.addFacesMessage("You must login before you can add cards to the inventory");
		}

	}

	public Card getCard() {
		return this.card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public int getAdd() {
		return add;
	}

	public void setAdd(int add) {
		this.add = add;
	}

	public BigDecimal getSpent() {
		return spent;
	}

	public void setSpent(BigDecimal spent) {
		this.spent = spent;
	}

	public Integer getAddCardId() {
		return addCardId;
	}

	public void setAddCardId(Integer addCardId) {
		card = em.find(Card.class, addCardId);
		this.addCardId = addCardId;
	}

	public BigDecimal getSellAmount() {
		return sellAmount;
	}

	public void setSellAmount(BigDecimal sellAmount) {
		this.sellAmount = sellAmount;
	}

	public int getKeep() {
		return keep;
	}

	public void setKeep(int keep) {
		this.keep = keep;
	}

}